﻿using System.Collections.Generic;
using System.Linq;
using OSC.Base;
using TweetSharp;

namespace OSC.Twitter
{
    public class TwitterSocialConnector : ISocialConnector
    {
        private const ulong ApplicationId = 123789887;
        private IPerson m_user;
        private TwitterService m_TwitterService;
        private OAuthRequestToken m_RequestToken;
        private OAuthAccessToken m_Token;
        public TwitterSocialConnector()
        {
        }

        #region Implementation of ISocialConnector
        public string LogonUrl
        {
            get
            {
                var key = "bxNFQyeake4KV2srK4B6iA";
                var secret = "8LmMqNN6IdxxPQyteMrwgBHNzSTOViCuK8y5PYJY6AE";
                m_TwitterService = new TwitterService(key, secret);
                m_RequestToken = m_TwitterService.GetRequestToken();
                var url = m_TwitterService.GetAuthenticationUrl(m_RequestToken);

                return url.ToString();
            }
        }
        public string SiteUrl
        {
            get { return "http://www.twitter.com"; }
        }
        public IPerson Logon(string username, string password, out string connectOut)
        {
            connectOut = m_RequestToken.ToString();
            m_Token = m_TwitterService.GetAccessToken(m_RequestToken);
            m_TwitterService.AuthenticateWith(m_Token.Token, m_Token.TokenSecret);
            m_user = ConvertTweeple(m_TwitterService.GetUserProfile());
            return m_user;
        }
        public void Follow(string[] emailAddresses, string displayName)
        {
            //throw new NotImplementedException();
        }
        public void Unfollow(string userId)
        {
            //throw new NotImplementedException();
        }
        public IEnumerable<IActivity> GetActivities(IPerson person)
        {
            return m_TwitterService.ListTweetsOnSpecifiedUserTimeline(int.Parse(person.UserId)).ToList()
                .Select(s =>
                            {
                                var activity = new Activity
                                                   {
                                                       ApplicationId = ApplicationId,
                                                       Id = s.Id.ToString(),
                                                       Owner = person,
                                                       PublishDate = s.CreatedDate,
                                                   };
                                activity.AddDetail(new Link("tweet",
                                                            string.Format("http://twitter.com/{0}/status/{1}",
                                                                          activity.Owner.Nickname,
                                                                          activity.Id),
                                                            s.Text));
                                activity.AddDetail(new Publisher(activity.Owner.UserId.ToString(),
                                                                 activity.Owner.FullName,
                                                                 activity.Owner.FullName,
                                                                 null,
                                                                 activity.Owner.WebProfilePage));
                                return activity;
                            }).Cast<IActivity>();
        }
        public IActivityTemplate GetActivityTemplate()
        {
            return new ActivityTemplate
                       {
                           ActivityType = ActivityTypes.StatusUpdate,
                           ApplicationId = ApplicationId,
                           Icon = "http://twitter-badges.s3.amazonaws.com/t_mini-b.png",
                           Title = "{link:tweet}"
                       };
        }
        public IEnumerable<IPerson> GetFriends()
        {
            return m_TwitterService.ListFriends().ToList().Select(ConvertTweeple);
        }
        private IPerson ConvertTweeple(TwitterUser u)
        {
            return new Person
                       {
                           FriendStatus = "friend",
                           FullName = u.Name,
                           Nickname = u.ScreenName,
                           PictureUrl = u.ProfileImageUrl,
                           UserId = u.Id.ToString(),
                           WebProfilePage = "http://twitter.com/" + u.ScreenName,
                       };
        }
        #endregion
    }
}